#include "teacher.h"
#include "ui_teacher.h"
#include<QMessageBox>
#include<QSqlQuery>
Teacher::Teacher(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Teacher)
{
    ui->setupUi(this);
    setWindowTitle("欢迎登录预约系统");
    //设置控件字体大小
    QFont ft;
    ft.setPointSize(12);
    ui->pushButton->setFont(ft);
    ui->pushButton_2->setFont(ft);
    ui->pushButton_3->setFont(ft);
    mode=new QSqlTableModel(this);
    mode->setTable("order_info");
    mode->setHeaderData(0,Qt::Horizontal,"预约序号");
    mode->setHeaderData(1,Qt::Horizontal,"用户ID");
    mode->setHeaderData(2,Qt::Horizontal,"姓名");
    mode->setHeaderData(3,Qt::Horizontal,"日期");
    mode->setHeaderData(4,Qt::Horizontal,"时间段");
    mode->setHeaderData(5,Qt::Horizontal,"机房号");
    mode->setHeaderData(6,Qt::Horizontal,"机房容量");
    mode->setHeaderData(7,Qt::Horizontal,"预约状态");
}

Teacher::~Teacher()
{
    delete ui;
}
    //查看所有预约
void Teacher::on_pushButton_clicked()
{
    ui->tableView->setModel(mode);
    mode->select();
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void Teacher::on_pushButton_2_clicked()
{
    ui->tableView->setModel(mode);
    mode->select();
    mode->setFilter("status='审核中'");
    ui->tableView->setEditTriggers(QAbstractItemView::NoEditTriggers);
}

void Teacher::on_pushButton_3_clicked()
{
    QItemSelectionModel *sMode=ui->tableView->selectionModel();
    QModelIndexList list=sMode->selectedRows();
    QAbstractItemModel *model=ui->tableView->model();
    QSqlQuery query;
    int flag=QMessageBox::warning(this,"审核","是否通过审核",QMessageBox::Yes,QMessageBox::No);
    if(flag==QMessageBox::Yes)
    {
        for(int i=0;i<list.size();i++)
        {
            QModelIndex index = model->index(list.at(i).row(),7);
            QVariant data = model->data(index);
            QString data1=data.toString();
            if(data1=="审核中")
            {
                //获取待审核记录的ID号
                QModelIndex index1 = model->index(list.at(i).row(),0);
                QVariant data2 = model->data(index1);
                int order_Id=data2.toInt();
                QString str=QString("update order_info set status='预约成功' where order_id=%1").arg(order_Id);
                query.exec(str);
            }
        }
    }
    else
    {
        for(int i=0;i<list.size();i++)
        {
            QModelIndex index = model->index(list.at(i).row(),7);
            QVariant data = model->data(index);
            QString data1=data.toString();
            if(data1=="审核中")
            {
                //获取待审核记录的ID号
                QModelIndex index1 = model->index(list.at(i).row(),0);
                QVariant data2 = model->data(index1);
                int order_Id=data2.toInt();
                QString str=QString("update order_info set status='预约失败' where order_id=%1").arg(order_Id);
                query.exec(str);
            }
        }
    }
}
